package org.jeecg.modules.demo.weixin.smtstoredvalueusers.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.weixin.entity.SmtStoredValueUsers;
import org.jeecg.modules.demo.weixin.smtstoredvalueusers.service.ISmtStoredValueUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: smt_stored_value_users
 * @Author: jeecg-boot
 * @Date: 2023-10-30
 * @Version: V1.0
 */
@Api(tags = "smt_stored_value_users")
@RestController
@RequestMapping("/weixin/smtstoredvalueusers/smtStoredValueUsers")
@Slf4j
public class SmtStoredValueUsersController extends JeecgController<SmtStoredValueUsers, ISmtStoredValueUsersService> {
    @Autowired
    private ISmtStoredValueUsersService smtStoredValueUsersService;

    /**
     * 分页列表查询
     *
     * @param smtStoredValueUsers
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "smt_stored_value_users-分页列表查询")
    @ApiOperation(value = "smt_stored_value_users-分页列表查询", notes = "smt_stored_value_users-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SmtStoredValueUsers>> queryPageList(SmtStoredValueUsers smtStoredValueUsers,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                            HttpServletRequest req) {
        QueryWrapper<SmtStoredValueUsers> queryWrapper = QueryGenerator.initQueryWrapper(smtStoredValueUsers, req.getParameterMap());
        Page<SmtStoredValueUsers> page = new Page<SmtStoredValueUsers>(pageNo, pageSize);
        IPage<SmtStoredValueUsers> pageList = smtStoredValueUsersService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param smtStoredValueUsers
     * @return
     */
//    @AutoLog(value = "smt_stored_value_users-添加")
    @ApiOperation(value = "smt_stored_value_users-添加", notes = "smt_stored_value_users-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SmtStoredValueUsers smtStoredValueUsers) {
        smtStoredValueUsersService.save(smtStoredValueUsers);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param smtStoredValueUsers
     * @return
     */
//    @AutoLog(value = "smt_stored_value_users-编辑")
    @ApiOperation(value = "smt_stored_value_users-编辑", notes = "smt_stored_value_users-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SmtStoredValueUsers smtStoredValueUsers) {
        smtStoredValueUsersService.updateById(smtStoredValueUsers);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
//    @AutoLog(value = "smt_stored_value_users-通过id删除")
    @ApiOperation(value = "smt_stored_value_users-通过id删除", notes = "smt_stored_value_users-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        smtStoredValueUsersService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
//    @AutoLog(value = "smt_stored_value_users-批量删除")
    @ApiOperation(value = "smt_stored_value_users-批量删除", notes = "smt_stored_value_users-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.smtStoredValueUsersService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "smt_stored_value_users-通过id查询")
    @ApiOperation(value = "smt_stored_value_users-通过id查询", notes = "smt_stored_value_users-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SmtStoredValueUsers> queryById(@RequestParam(name = "id", required = true) Integer id) {
        SmtStoredValueUsers smtStoredValueUsers = smtStoredValueUsersService.getById(id);
        if (smtStoredValueUsers == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(smtStoredValueUsers);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param smtStoredValueUsers
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SmtStoredValueUsers smtStoredValueUsers) {
        return super.exportXls(request, smtStoredValueUsers, SmtStoredValueUsers.class, "smt_stored_value_users");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SmtStoredValueUsers.class);
    }


    @GetMapping(value = "/queryValue")
    public SmtStoredValueUsers queryValue(@RequestParam(name = "userId", required = true) Integer userId, @RequestParam(name = "merId", required = true) String merId) {
        LambdaQueryWrapper<SmtStoredValueUsers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmtStoredValueUsers::getUserId, userId);
        queryWrapper.eq(SmtStoredValueUsers::getMerchantId, Integer.valueOf(merId));
        SmtStoredValueUsers smtStoredValueUsers = smtStoredValueUsersService.getOne(queryWrapper);
        return smtStoredValueUsers;
    }


    @GetMapping(value = "/queryValueCount")
    public List<SmtStoredValueUsers> queryValueCount(@RequestParam(name = "userId", required = true) Integer userId, @RequestParam(name = "merId", required = true) String merId) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LambdaQueryWrapper<SmtStoredValueUsers> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmtStoredValueUsers::getUserId, userId);
        queryWrapper.eq(SmtStoredValueUsers::getStatus, 0);
        queryWrapper.eq(SmtStoredValueUsers::getMerchantId, Integer.valueOf(merId));
        List<SmtStoredValueUsers> list = smtStoredValueUsersService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        List<SmtStoredValueUsers> counts = new ArrayList<>();
        list.forEach(value -> {
            if (!value.getValueValidDate().equals("无限制")) {
                if (LocalDate.parse(value.getValueValidDate(), dateTimeFormatter).compareTo(LocalDate.now()) > 0) {
                    counts.add(value);
                }
                else {
                    //1 是过期了
                    value.setStatus(1);
                    //进行修改
                    smtStoredValueUsersService.updateById(value);
                }
            }

        });
        return list;
    }

}
